<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of account
 *
 * @author b4by
 */
class account extends MY_Controller {

	function __construct() {
		parent::__construct();
	}

	public function index() {
		
	}

	public function register() {
		if (isset($_POST['email'])) {
			$this->load->helper('form');
			$this->load->library('form_validation');

			$this->form_validation->set_rules('username', 'lang:username', 'required|min_length[5]|max_length[50]|callback__check_username');
			$this->form_validation->set_rules('password', 'lang:password', 'required|min_length[5]|max_length[30]');
			$this->form_validation->set_rules('password1', 'lang:password_confirm', 'required|matches[password]');
			$this->form_validation->set_rules('mobile', 'lang:mobile', 'required|min_length[8]|max_length[20]');
			$this->form_validation->set_rules('fullname', 'lang:fullname', 'min_length[8]|max_length[100]');
			$this->form_validation->set_rules('address', 'lang:address', 'min_length[8]');
			$this->form_validation->set_rules('email', 'lang:email', 'required|valid_email|callback__check_email');
			$this->form_validation->set_rules('id_no', 'lang:Identification Number', 'required|min_length[9]|max_length[20]');
			$this->form_validation->set_rules('sponsor', 'lang:Sponsor', 'required|max_length[100]');
//		$this->form_validation->set_rules('agree', 'lang:agree_terms', 'required|callback__check_agree_terms');
//		$this->form_validation->set_rules('gender','lang:gender','callback__check_gender');


			if ($this->form_is_valid()) {
				$data = request_filter(array('username', 'password', 'fullname', 'mobile', 'email', 'address', 'id_no', 'sponsor'));
				$data['active'] = 1;
				$data['group_id'] = $this->siteConfig->default_user_group;
				$this->model_user->create($data);

				echo json_encode(array(
					'state' => 'success',
					'message' => lang('Registration success!'),
					'redirect' => url_create('site', 'account', 'login'),
				));
			} else {
				echo json_encode(array(
					'state' => 'error',
					'errors' => $this->data['errors']
				));
			}
		} else {
			$this->data['temp'] = 'account/register';
			$this->load_view();
		}
	}

	public function _check_username($username) {
		$info = $this->model_user->get_info_rule(array(
			'username' => $username,
		));
		if ($info === FALSE)
			return TRUE;
		$this->form_validation->set_message(__FUNCTION__, lang('Username has been used!'));
		return FALSE;
	}

	public function _check_email($email) {
		$info = $this->model_user->get_info_rule(array(
			'email' => $email,
		));
		if ($info === FALSE)
			return TRUE;
		$this->form_validation->set_message(__FUNCTION__, lang('email_used'));
		return FALSE;
	}

	public function _check_agree_terms($value) {
		if ($value == 1)
			return TRUE;
		$this->form_validation->set_message(__FUNCTION__, lang('agree_terms_notify'));
		return FALSE;
	}

	public function login() {
		if (isset($_POST['username'])) {
			$this->load->helper('form');
			$this->load->library('form_validation');

			$this->form_validation->set_rules('username', 'lang:username', 'required');
			$this->form_validation->set_rules('password', 'lang:password', 'required|callback__check_login');


			$ref = url_create('site', 'home', 'index');
			if ($this->input->get('ref'))
				$ref = $this->input->get('ref');
			if ($this->form_is_valid()) {
				echo json_encode(array(
					'state' => 'success',
					'message' => lang('Login success!'),
					'redirect' => $ref,
				));
			} else {
				$data = array(
					'state' => 'error',
					'errors' => $this->data['errors'],
				);
				echo json_encode($data);
			}
		} else {
			$this->data['temp'] = 'account/login.php';
			$this->load_view();
		}
	}

	public function _check_login($value) {
		if (user_login($this->input->post('username'), $this->input->post('password'), $this->input->post('remember')))
			return TRUE;
		$this->form_validation->set_message(__FUNCTION__, lang('login_fail'));
		return FALSE;
	}

	public function fb_login() {

		$ref = request('ref');

		if ($ref == '')
			$ref = url_create('site');

		if (preg_match('/' . addcslashes(url_create('site', 'account', 'fb_login'), '/.\\*+[])(|?^') . '/i', $ref) != 0)
			$ref = url_create('site');

		if ($this->isUserLogin())
			redirect($ref);

		$this->load->library('facebook');
		$this->load->model('model_user');

		$code = request('code');

		if (strlen($code) == 0) {
			redirect($this->facebook->getLoginUrl(array(
						'scope' => 'email,user_about_me'
			)));
		}
		$graph_url = "https://graph.facebook.com/me?access_token="
				. $this->facebook->getAccessToken();

		$user = json_decode(file_get_contents($graph_url));

		$fb_userId = $user->id;

		if ($fb_userId == 0) {
			echo 'Not authentication';
			exit;
		}
		$info = $this->model_user->get_info_rule(array('fb_id' => $fb_userId));

		if ($info !== false) {
			$this->model_user->update($info->id, array(
				'username' => $user->username,
				'fullname' => $user->name,
			));
			$this->session->set_userdata('user_id', $info->id);
			redirect($ref);
		}

		$user_id = $this->model_user->create(array(
			'email' => $user->email,
			'username' => $user->username,
			'fb_id' => $fb_userId,
			'active' => 1,
			'group_id' => $this->siteConfig->default_user_group,
			'fullname' => $user->name,
			'join_date' => now()
		));

		$this->session->set_userdata('user_id', $user_id);

		redirect($ref);
	}

	public function logout() {

		user_logout();
		redirect(url_create('site'));
	}

	public function info($id = 0) {
		
		$id = (int) $id;
		if ($id == 0)
			$id = $this->user->id;
		if ($this->isAdmin() || $id == $this->user->id) {
			$this->load->model('model_page_user');
			$info = $this->model_user->get_info($id);
			$info->password = '';
			$registed_pages = $this->model_page_user->get_registed_pages(array(
				'where'	=>array('user_id'	=> $id),
			));
			$this->data['registed_pages'] = $registed_pages;
			$this->data['info'] = $info;
			$this->data['temp'] = 'account/info';
			$this->load_view();
		}
		else{
			show_error(lang('You don\'t have permission to view this page!'));
		}
	}

}

?>
